package org.ysu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.ysu.mapper.PoorMapper;
import org.ysu.pojo.Family;
import org.ysu.mapper.FamilyMapper;
import org.springframework.stereotype.Service;
import org.ysu.pojo.Poor;
import org.ysu.pojo.PoorFamily;
import org.ysu.pojo.User;
import org.ysu.service.FamilyService;

import javax.annotation.Resource;
import java.util.List;

/**
* @author libaoyu
* @description 针对表【family】的数据库操作Service实现
* @createDate 2024-06-17 14:59:51
*/
@Service
public class FamilyServiceImpl extends ServiceImpl<FamilyMapper, Family>
    implements FamilyService {
    @Resource
    private FamilyMapper familyMapper;
    @Autowired
    private PoorMapper poorMapper;

    /*
    * 根据id查询对象
    * */
    @Override
    public Family getFamily(Integer id) {
        //根据id查询对象
        Family family = familyMapper.getFamily(id);
        if(family!=null){
            return family;
        }else{
            family.setFId(-1);
            return family;
        }
    }

    /*
    * 根据省市县查询对象
    * */
    @Override
    public List<Family> getListByRegion(String province, String city, String country) {
        LambdaQueryWrapper<Family> wrapper=new LambdaQueryWrapper<>();
        //当province、city和country不为空时，才能作为条件进行查询
        wrapper.eq(StringUtils.isNotBlank(province),Family::getProvince,province)
                .eq(StringUtils.isNotBlank(city),Family::getCity,city)
                .eq(StringUtils.isNotBlank(country),Family::getCountry,country);
        List<Family> list=familyMapper.selectList(wrapper);
        System.out.println(list);
        return list;
    }

    /*
    * 查询所有家庭对象
    * */
    @Override
    public List<Family> getAllFamilys() {
        //查询所有家庭对象
        LambdaQueryWrapper<Family> wrapper=new LambdaQueryWrapper<>();
        List<Family> list=familyMapper.selectList(wrapper);
        return list;
    }

    /**
     * 通过vcid查家庭
     * @param vcid
     * @return
     */
    @Override
    public List<Family> getFamilysByvcid(Integer vcid){
        LambdaQueryWrapper<Family> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Family::getVcId,vcid);
        List<Family> list=familyMapper.selectList(wrapper);
        return list;
    }

    @Override
    public void addFamily(PoorFamily poorFamily){
        //添加到family中，获取fid
        Family family=new Family();
        family.setProvince(poorFamily.getProvince());
        family.setCity(poorFamily.getCity());
        family.setCountry(poorFamily.getCountry());
        family.setTownship(poorFamily.getAddress());
        family.setPoverty(poorFamily.getPoverty());
        family.setIntroduce(poorFamily.getIntroduce());
        familyMapper.addFamily(family);
        int fid=family.getFId();
        if(fid>0){
            //添加到poor中
            for(int i=0;i<poorFamily.getPoors().size();i++){
                Poor poor=poorFamily.getPoors().get(i);
                poor.setFId(fid);
                poor.setIshead(0);
                poorMapper.addPoor(poor);
            }
        }
        Poor head=new Poor();
        head.setFId(fid);
        head.setIshead(1);

        head.setAge(poorFamily.getAge());
        head.setHealth(poorFamily.getHealth());
        head.setName(poorFamily.getName());
        head.setNation(poorFamily.getNation());
        head.setPhone(poorFamily.getPhone());
        head.setWork(poorFamily.getWork());
        head.setAvator(poorFamily.getPhoto());
        head.setIdnumber(poorFamily.getIdNumber());
        head.setBasic(poorFamily.getBasic());
        head.setInsurance(poorFamily.getInsurance());
        head.setRelation("户主");
        poorMapper.addPoor(head);
        int pid=head.getPId();
        familyMapper.updatePid(fid,pid);
    }
}




